﻿using System;
using System.Collections.Generic;
using System.Text;
using JWFW.Encrypt;

namespace JWFW.SFL
{
    public class EncryptOP
    {
        /// <summary>
        /// 防篡改加密字符串
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public static string EncryptString(string input)
        {
            return MD5.AddMD5Profix(Base64.Encrypt(MD5.AddMD5Profix(input)));
        }
        /// <summary>
        /// 解密加过密的字符串
        /// </summary>
        /// <param name="input"></param>
        /// <param name="needValidate">是否需要验证，默认True</param>
        /// <param name="throwException">解密失败是否抛异常，默认True</param>
        /// <returns></returns>
        public static string DecryptString(string input, bool needValidate = true, bool throwException = true)
        {
            string res = "";
            try
            {
                res = input;
                if (!needValidate ||
                    (needValidate && MD5.ValidateValue(res)))
                {
                    return MD5.RemoveMD5Profix(Base64.Decrypt(MD5.RemoveMD5Profix(res)));
                }
                else
                {
                    throw new Exception("字符串无法转换成功！");
                }
            }
            catch
            {
                if (throwException)
                {
                    throw;
                }
                else
                {
                    return "";
                }

            }
        }
        /// <summary>
        /// 随机(1-3)次进行防篡改加密
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public static string RandomMultiTimesEncrypt(string input)
        {
            string randomChar = CommonOP.GetRandomChar();
            int times = (Char.ConvertToUtf32(randomChar, 0) % 3 + 1);
            for (int i = 0; i < times; i++)
            {
                input = EncryptString(CommonOP.GetRandomChar() + input + CommonOP.GetRandomChar());
            }
            input = randomChar + input;
            return input;
        }
        /// <summary>
        /// 移除随机的多次防篡改加密
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public static string RemoveRandomMultiRimesEncrypt(string input)
        {
            if (input.Length > 0)
            {
                int times = (Char.ConvertToUtf32(input[0].ToString(), 0) % 3 + 1);
                input = input.Remove(0, 1);
                for (int i = 0; i < times; i++)
                {
                    input = DecryptString(input);
                    input = input.Remove(input.Length - 1, 1).Remove(0, 1);
                }
            }
            return input;
        }
        /// <summary>
        /// 对字符串做植入随机字符处理，由随机字符确定隔3个还是4个字符前植入
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public static string InsertRandom(string input)
        {
            StringBuilder res = new StringBuilder();
            string tmp = "";
            int interval = 0;
            int lastPos = 0;
            for (int i = 0; i < input.Length; i++)
            {
                if (i - lastPos == interval)
                {
                    tmp = CommonOP.GetRandomChar();
                    interval = char.ConvertToUtf32(tmp, 0) % 2 + 3;
                    lastPos = i;
                    res.Append(tmp);
                }
                res.Append(input[i]);
            }
            res.Append(CommonOP.GetRandomChar());
            return res.ToString();
        }
        /// <summary>
        /// 移除插入的随机字符
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public static string RemoveInsertRandom(string input)
        {
            StringBuilder res = new StringBuilder();
            int interval = 0;
            int lastPos = 0;
            for (int i = 0; i < input.Length; i++)
            {
                if (i == lastPos + interval)
                {
                    interval = char.ConvertToUtf32(input, i) % 2 + 3;
                    lastPos = i + 1;
                }
                else if (i != input.Length - 1)
                {
                    res.Append(input[i]);
                }
            }
            return res.ToString();
        }
        /// <summary>
        /// 随机的反转字符串
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public static string RandomReverse(string input)
        {
            string tmp = CommonOP.GetRandomChar();
            bool reverse = char.ConvertToUtf32(tmp, 0) % 2 == 0;
            if (reverse)
            {
                input = CommonOP.ReverseString(input);
            }
            return tmp + input;
        }
        /// <summary>
        /// 恢复随机的反转字符串
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public static string RemoveRandomReverse(string input)
        {
            if (string.IsNullOrEmpty(input))
            {
                return "";
            }
            bool reverse = char.ConvertToUtf32(input, 0) % 2 == 0;
            input = input.Remove(0, 1);
            if (reverse)
            {
                input = CommonOP.ReverseString(input);
            }
            return input;
        }

        private EncryptOP()
        {
        }
        private void Test()
        {
            string a = "";
            a = "1234567890";
            string t = InsertRandom(a);
            string i = RemoveInsertRandom(t);
            Console.WriteLine(t);
            Console.WriteLine(i);

            t = RandomReverse(i);
            i = RemoveRandomReverse(t);
            Console.WriteLine(t);
            Console.WriteLine(i);

            t = EncryptString(a);
            i = DecryptString(t);
            Console.WriteLine(t);
            Console.WriteLine(i);

            t = RandomMultiTimesEncrypt(a);
            i = RemoveRandomMultiRimesEncrypt(t);
            Console.WriteLine(t);
            Console.WriteLine(i);
        }
    }
}
